package com.cloud.pole.shop.controller.shops;

import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.util.RegexUtils;
import com.cloud.pole.shop.controller.goods.GoodsCategoryController;
import com.cloud.pole.shop.model.entity.shops.ShopShopsView;
import com.cloud.pole.shop.model.vo.IndexVo;
import com.cloud.pole.shop.model.vo.goods.GoodsCategoryTree;
import com.cloud.pole.shop.model.vo.shops.MyShopVo;
import com.cloud.pole.shop.model.vo.shops.ShopIndexVo;
import com.cloud.pole.shop.service.shops.ShopShopsService;

import lombok.extern.slf4j.Slf4j;

/*
*2019年6月6日
*cloud-shop
*author:frank-chen
*/
@Controller
@Slf4j
@RequestMapping("/shops/front")
public class ShopsController {

	@Autowired 
	private ShopShopsService shopShopsService;
	
	@ResponseBody
	@RequestMapping("/createShopWx")
    public ApiRestResponse<Integer> createShopWx(
    		@RequestParam(name="banner", defaultValue="") String banner,
    		@RequestParam(name="shopName", defaultValue="") String shopName,
    		@RequestParam(name="shopOwnerName", defaultValue="") String shopOwnerName,
    		@RequestParam(name="mobileArea", defaultValue="86") String mobileArea,
    		@RequestParam(name="shopOwnerMobile", defaultValue="") String shopOwnerMobile,
    		@RequestParam(name="openId", defaultValue="") String openId){   	
    	try {
			ParamException.err(StringUtils.isBlank(banner), "banner不能为空");   
			ParamException.err(!RegexUtils.isImageFile(banner), "图片格式错误");   
			ParamException.err(StringUtils.isBlank(shopName), "shopName不能为空");    		
			ParamException.err(StringUtils.isBlank(shopOwnerName), "shopOwnerName不能为空");
			ParamException.err(StringUtils.isBlank(shopOwnerMobile), "shopOwnerMobile不能为空");
			ParamException.err(StringUtils.isBlank(openId), "openId不能为空");
			ParamException.err(!RegexUtils.isMobileNo(shopOwnerMobile, mobileArea), "手机号错误");
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, shopShopsService.createShopWx(banner, shopName, shopOwnerName, mobileArea, shopOwnerMobile, openId));

    	} catch (BaseException e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	@ResponseBody
	@RequestMapping("/index")
    public ApiRestResponse<ShopIndexVo> getIndexData(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestHeader(defaultValue="") String shopId,
    		@RequestParam(defaultValue="") String openId,
    		@RequestParam(defaultValue="") String recommId){   	
    	try {
			ParamException.err(StringUtils.isBlank(openId), "openId不能为空");   
			Integer shopIdInt = null;
			if(RegexUtils.isPositiveInteger(shopId) && !StringUtils.isEmpty(shopId)) {
				shopIdInt = Integer.parseInt(shopId);
			}
			Long recommUserId = null;
			if(RegexUtils.isPositiveInteger(recommId) && !StringUtils.isEmpty(recommId)) {
				recommUserId = Long.parseLong(recommId);
			}
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, shopShopsService.getIndexData(token, shopIdInt, openId, recommUserId));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	/**
	 * 粉丝关注状态
	 * @param token
	 * @param shopId
	 * @param openId
	 * @param opType 0：取消 1：关注
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/modifyFansStatus")
    public ApiRestResponse<Long> modifyFansStatus(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestHeader(defaultValue="") String shopId,
    		@RequestParam(defaultValue="") String openId,
    		@RequestParam(defaultValue="") String fansStatus){   	
    	try {
			ParamException.err(StringUtils.isBlank(shopId), "shopId不能为空");    		
			ParamException.err(StringUtils.isBlank(openId), "openId不能为空");    		
			ParamException.err(StringUtils.isBlank(fansStatus), "fansStatus不能为空");    		
			ParamException.err(!RegexUtils.isPositiveInteger(shopId), "shopId必须数字类型");
			ParamException.err(!RegexUtils.isInteger(fansStatus), "fansStatus必须数字类型");
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, shopShopsService.modifyFansStatus(token, Integer.parseInt(shopId), openId, Integer.parseInt(fansStatus)));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	@ResponseBody
	@RequestMapping("/getWxUserQrCode")
    public ApiRestResponse<String> getWxUserQrCode(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestHeader(defaultValue="") String shopId,
    		@RequestParam(defaultValue="") String openId){   	
    	try {
			ParamException.err(StringUtils.isBlank(shopId), "shopId不能为空");    		
			ParamException.err(StringUtils.isBlank(openId), "openId不能为空");    		
			ParamException.err(!RegexUtils.isPositiveInteger(shopId), "shopId必须数字类型");
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, shopShopsService.getWxUserQrCode(token, Integer.parseInt(shopId), openId));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	@ResponseBody
	@RequestMapping("/getUserViewShopList")
    public ApiRestResponse<List<ShopShopsView>> getUserViewShopList(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestParam(defaultValue="") String openId){   	
    	try {
			ParamException.err(StringUtils.isBlank(openId), "openId不能为空");   
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, shopShopsService.getUserViewShopList(token, openId));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
}
